// Fill out your copyright notice in the Description page of Project Settings.


#include "Weapon/TULauncherWeapon.h"
#include "Weapon/TUProjectile.h"
#include "Kismet/GameplayStatics.h"
#include "Sound/SoundCue.h"


void ATULauncherWeapon::StartFire()
{
    Super::StartFire();

    MakeShot();
}

void ATULauncherWeapon::MakeShot()
{
    if (!GetWorld() || IsAmmoEmpty())
    {
        StopFire();
        return;
    }

    FVector TraceStart;
    FVector TraceEnd;
    GetTraceData(TraceStart, TraceEnd);
    FHitResult HitResult;
    MakeHit(HitResult, TraceStart, TraceEnd);

    const FVector EndPoint = HitResult.bBlockingHit ? HitResult.ImpactPoint : TraceEnd;
    const FVector Direction = (EndPoint - GetMuzzleWorldLocation()).GetSafeNormal();

    const FTransform SpawnTransform(FRotator::ZeroRotator, GetMuzzleWorldLocation());
    ATUProjectile* Projectile = GetWorld()->SpawnActorDeferred<ATUProjectile>(ProjectileClass, SpawnTransform);

    if (Projectile)
    {
        Projectile->SetShotDirection(Direction);
        Projectile->SetOwner(GetOwner());
        Projectile->FinishSpawning(SpawnTransform);
    }

    DecreaseAmmo();

    SpawnMuzzleFX();
    StopFire();

}






















